/* eslint-disable */

export default cc.Class({
    extends: cc.Component,

    properties: {
        frames: null,
        loop: false,
        sample: 10,
        onEndCb: null,
        prename:"",
    },

    start() {
        this.draw();
        
        // var animation = this.node.getComponent(cc.Animation);
        // currentClip
    },

    draw() {
        //console.log('[Anim Component]: animation draw');
        var animation = this.node.getComponent(cc.Animation);
        if (!animation) {
            console.error('[Anim Component]: cc.Animation not found');
            return;
        }

        if (!this.frames) {
            console.error('[Anim Component]: atlas not found');
            return;
        }

        var frames = [];
        for (var i = 1; ; i++) {
            let frame = this.frames[this.prename + i + '.png'];
            if (!frame) {
                break;
            }
            frames[i - 1] = frame;
        }

        if (frames.length > 1) {
            //console.log('[Anim Component]: animation draw');
            var clip = cc.AnimationClip.createWithSpriteFrames(
                frames,
                this.sample
            );
            clip.name = 'anim_run';
            if (this.loop) {
                clip.wrapMode = cc.WrapMode.Loop;
            } else {
                animation.once('finished', this.onFinished, this);
            }
            animation.addClip(clip);
            animation.play('anim_run');
        }
    },

    onFinished() {
        //console.log('[Anim Component]: animation finished');
        this.node.emit('finished', this);
        if (this.onEndCb) {
            this.onEndCb(this);
        }
    },
});
